package com.google.code.iproc;

import java.io.IOException;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfWriter;

class HeaderFooter extends PdfPageEventHelper {
	public final FontFactory fontFactory;
	Font headerFooterFont;
	Phrase[] header = new Phrase[2];
	
	public HeaderFooter( final FontFactory fontFactory ) {
		this.fontFactory = fontFactory;
	}

	public void setHeaders( final String s ) {
		setHeaderOdd( s );
		setHeaderEven( s );
	}
	
	public void setHeaderOdd( final String s ) {
		header[1] = new Phrase( s, headerFooterFont );
	}

	public void setHeaderEven( final String s ) {
		header[0] = new Phrase( s, headerFooterFont );
	}
	
	public void onOpenDocument(PdfWriter writer, Document document) {
		try {
			headerFooterFont = fontFactory.getTimesNewRoman( 7 );
			header[0] = new Phrase("Book of proceedings", headerFooterFont );
			header[1] = new Phrase("Paper title", headerFooterFont );
		} catch (DocumentException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

//	public void onChapter(PdfWriter writer, Document document,
//			float paragraphPosition, Paragraph title) {
//	}

	public void onStartPage(PdfWriter writer, Document document) {
		document.setMargins( IprocProcessor.MARGIN_INNER, IprocProcessor.MARGIN_OUTER, IprocProcessor.MARGIN_TOP, IprocProcessor.MARGIN_BOTTOM );
		document.setMarginMirroring( true );
	}

	private boolean omitPageNumber = false;
	
	public void omitPageNumber() {
		omitPageNumber = true;
	}
	
	public void onEndPage(PdfWriter writer, Document document) {
		final boolean putFooter = ( writer.getPageNumber() > 1 ) && ! omitPageNumber;
		final boolean putHeader = ( writer.getPageNumber() > 1 );

		omitPageNumber = false;
		Rectangle rect = IprocProcessor.PAGE_CONTENTS_RECT[ writer.getPageNumber() % 2 ];
		final PdfContentByte contents = writer.getDirectContentUnder();
		final float shiftY = 4;
		final float ruleElevation = 20;
		final float outerMargin = 0;
		final float pageNumberDepression = 30;
		
		if ( putHeader ) {
			switch (writer.getPageNumber() % 2) {
			case 1:
				ColumnText.showTextAligned(contents,
						Element.ALIGN_RIGHT, header[1], rect.getRight() - outerMargin,
						rect.getTop() + shiftY + ruleElevation, 0 );
				break;
			case 0:
				ColumnText.showTextAligned(contents,
						Element.ALIGN_LEFT, header[0], rect.getLeft() + outerMargin,
						rect.getTop() + shiftY + ruleElevation, 0);
				break;
			}
			contents.saveState();
			contents.setLineWidth( 0.2f );
			contents.setGrayStroke( 0.6f );
			contents.moveTo( rect.getLeft(), rect.getTop() + ruleElevation );
			contents.lineTo( rect.getRight(), rect.getTop() + ruleElevation );
			contents.stroke();
			contents.restoreState();
		}
		if ( putFooter ) {
			ColumnText.showTextAligned(contents,
					Element.ALIGN_CENTER,
					new Phrase(String.format("%d", document.getPageNumber()), headerFooterFont),
					(rect.getLeft() + rect.getRight()) / 2, rect.getBottom() - pageNumberDepression,
					0);
		}
	}
}